package com.qdlc.p2p.biz.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.executer.AbstractExecuter;
import com.qdlc.p2p.biz.executer.ExecuterHelper;
import com.qdlc.p2p.biz.executer.impl.UserRegisterExecuter;
import com.qdlc.p2p.biz.service.CooperationLoginService;
import com.qdlc.p2p.biz.util.mail.Mail;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.dao.*;
import com.qdlc.p2p.dal.dto.*;

@Service("cooperationLoginService")
public class CooperationLoginServiceImpl implements CooperationLoginService {

    @Resource
    private CooperationLoginDao cooperationLoginDao;

    @Resource
    private UserLoginExtDao userLoginExtDao;

    @Resource
    private UserDao userDao;

    @Resource
    private UserIdentifyDao userIdentifyDao;

    @Resource
    private UserCacheDao userCacheDao;

    @Resource
    private AccountDao accountDao;

    @Resource
    private UserCreditDao userCreditDao;

    @Resource
    private ScoreDao scoreDao;

    @Override
    public void addCooperationLogin(CooperationLogin cooperation) {
        // 添加联合登录信息时，须先判断是否已有联合登录的记录信息，如果无，则添加
        CooperationLogin cooperationLogin = cooperationLoginDao.getCooperationLogin(cooperation.getOpenId(),
                cooperation.getType());
        if (cooperationLogin == null) {
            cooperationLoginDao.save(cooperation);
        }

    }

    @Override
    public CooperationLogin getCooperationLogin(String openId, int type) {
        return cooperationLoginDao.getCooperationLogin(openId, type);
    }

    @Override
    public User doQQRegister(User user, String openType, String openId) {
        user = this.doRegister(user);
        CooperationLogin coop = cooperationLoginDao.getCooperationLogin(openId, Integer.parseInt(openType));
        coop.setUserId(user.getUserId());
        cooperationLoginDao.update(coop);
        return user;
    }

    public User doRegister(User user) {
        int hasEmail = userDao.countByEmail(user.getEmail());
        if (hasEmail > 0) {
            throw new UserException("邮箱已经被使用.", 1);
        }

        int hasUsername = userDao.countByUserName(user.getUserName());
        if (hasUsername > 0) {
            throw new UserException("用户名已经被使用.", 1);
        }

        user.setAddTime(DateUtil.getNow());

        // 登录扩展表
        UserLoginExt userLoginExt = new UserLoginExt(user);
        // 用户名插入
        userLoginExt.setUserName(user.getUserName());
        userLoginExt.setAddTime(user.getAddTime());
        userLoginExtDao.save(userLoginExt);

        // 手机号插入
        userLoginExt = new UserLoginExt(user);
        userLoginExt.setUserName(user.getMobilePhone());
        userLoginExt.setAddTime(user.getAddTime());
        userLoginExtDao.save(userLoginExt);
        // user = userDao.save(user);

        // 用户认证信息
        userIdentifyDao.save(new UserIdentify(user.getUserId()));
        // 用户附属信息
        userCacheDao.save(new UserCache(user, Global.getIP()));
        // 资金账户
        accountDao.save(new Account(user.getUserId()));
        // 用户amount
        userCreditDao.save(new UserCredit(user));
        // 添加用户积分信息
        scoreDao.save(new Score(user));

        // 发送激活邮件
        Global.setTransfer("activeUrl", "/user/doRegisterStep1.html?id=" + Mail.getInstance().getdecodeIdStr(user));
        Global.setTransfer("user", user);
        AbstractExecuter executer = ExecuterHelper.getExecuter(UserRegisterExecuter.class);
        executer.execute(0, user);
        return user;
    }
}
